//
//  YZFormView.m
//  kuaiyi
//
//  Created by WGP on 16/5/7.
//
//

#import "YZFormView.h"

#define widthOfMine self.bounds.size.width
#define heightOfMine self.bounds.size.height
#define kWidth  (widthOfMine / 8)
#define kHeight (kWidth + 8)
#define kMargin 4

NSString * const kWeek = @"week";
NSString * const kDate = @"date";
NSString * const kTime = @"time";

@interface YZFormView ()

@property(nonatomic,strong)NSIndexPath *currentSelected;

@end
@implementation YZFormView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self prepareUI];
    }
    return self;
}

-(void)prepareUI
{
    self.backgroundColor = [UIColor whiteColor];
    self.row = 4;
    self.column = 8;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClick:)];
    [self addGestureRecognizer:tap];
}

-(void)didClick:(UITapGestureRecognizer *)tap
{
    CGPoint clickPoint = [tap locationInView:self];
    
    NSInteger column = clickPoint.x / kWidth;
    NSInteger row = clickPoint.y / kHeight;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:column inSection:row];
    if (row == 0 || column == 0) {
        return ;
    }
    
    if ([self.currentSelected isEqual:indexPath]) {
        self.currentSelected = nil;
        return ;
    }
    self.currentSelected = indexPath;
    
    if ([self.delegate  respondsToSelector:@selector(formDidClick:hasNumber:info:)]) {
        
      __block  BOOL hasNumber = NO;
        [self.hasNumberArr enumerateObjectsUsingBlock:^(NSIndexPath * index, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([index isEqual:indexPath]) {
                hasNumber = YES;
                *stop = YES;
            }
        }];
        
        NSDictionary *info = @{
                               kWeek : [self firstRowText][indexPath.row-1],
                               kDate : [self firstRowDateText][indexPath.row-1],
                               kTime : [self firstColumnText][indexPath.section-1],
                               };
        [self.delegate formDidClick:self hasNumber:hasNumber info:info];
    }
}

-(void)drawRect:(CGRect)rect
{
    /** 背景色*/
    UIBezierPath *bgColorPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, rect.size.width, kHeight)];
    [[UIColor colorWithWhite:0.9 alpha:0.5] set];
    [bgColorPath fill];
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    path.lineWidth = 0.2;
    [[UIColor lightGrayColor] set];
    /** 横线*/
    [path moveToPoint:CGPointMake(0, 1)];
    for (NSInteger i = 0; i < _row + 1; i++) {
        [path addLineToPoint:CGPointMake(rect.size.width, i == 0 ? 1 : i*kHeight)];
        [path stroke];
        [path moveToPoint:CGPointMake(0, (i+1)*kHeight)];
    }
    
    /** 竖线*/
    [path moveToPoint:CGPointMake(1, 0)];
    for (NSInteger i = 0; i < _column + 1; i++) {
        [path addLineToPoint:CGPointMake(i == 0 ? 1 : i*kWidth, rect.size.height)];
        [path stroke];
        [path moveToPoint:CGPointMake((i+1)*kWidth, 0)];
    }
      /** 第一行文字*/
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18]};
    [[self firstRowText] enumerateObjectsUsingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [text drawAtPoint:CGPointMake((idx + 1) * kWidth + kMargin * 1.5, kMargin) withAttributes:attributes];
        [[self firstRowDateText][idx] drawAtPoint:CGPointMake((idx + 1) * kWidth + kMargin * 2, kHeight * 0.5 + kMargin * 2) withAttributes:nil];
    }];
    
    /** 第一列文字*/
    [[self firstColumnText] enumerateObjectsUsingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [text drawAtPoint:CGPointMake(kMargin * 3, (idx + 1) * kHeight + kMargin * 5) withAttributes:nil];
    }];
    
    /** 高亮*/
    if (self.currentSelected.row || self.currentSelected.section) {
        CGRect rectFrame = CGRectMake(self.currentSelected.row * kWidth, self.currentSelected.section * kHeight, kWidth, kHeight);
        UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:rectFrame];
        [[UIColor greenColor] set];
        [rectPath fill];
    }
    
    /** 有号*/
    [[self hasNumberArr] enumerateObjectsUsingBlock:^(NSIndexPath * index, NSUInteger idx, BOOL * _Nonnull stop) {
        [@"有号" drawAtPoint:CGPointMake(kMargin * 3 + index.row * kWidth, index.section * kHeight + kMargin * 5) withAttributes:nil];
    }];
}

- (void)setRow:(NSUInteger)row
{
    _row = row;
    [self setFrame:({
        CGRect frame      = self.frame;
        frame.size.height = row * kHeight;
        if (frame.size.height > heightOfMine) {
            frame.size.height = heightOfMine;
        }
        frame;
    })];
    [self setNeedsDisplay];
}

- (void)setColumn:(NSUInteger)column
{
    _column = column;
    [self setFrame:({
        CGRect frame     = self.frame;
        frame.size.width = column * kWidth;
        if (frame.size.width > widthOfMine) {
            frame.size.width = widthOfMine;
        }
        frame;
    })];
    [self setNeedsDisplay];
}

-(void)setCurrentSelected:(NSIndexPath *)currentSelected
{
    _currentSelected = currentSelected;
    [self setNeedsDisplay];
}

-(NSArray *)firstRowText
{
    return @[@"周日",
             @"周一",
             @"周二",
             @"周三",
             @"周四",
             @"周五",
             @"周六",];
}

- (NSArray *)firstRowDateText
{
    return @[@"05-05",
             @"05-06",
             @"05-07",
             @"05-08",
             @"05-09",
             @"05-10",
             @"05-11",];
}

- (NSArray *)firstColumnText
{
    return @[@"上午",
             @"下午",
             @"晚上"];
}

@end







